package com.newway.back.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.newway.back.entity.NearbyPlaces;
import com.newway.back.mapper.NearbyPlacesMapper;
import com.newway.common.context.UserInfo;
import com.newway.common.context.UserInfoContext;
import com.newway.common.exception.BusinessException;
import com.newway.common.mapper.UserInfoMapper;
import com.newway.common.result.Result;
import com.newway.wechat.dto.PlaceBookingRecord;
import com.newway.wechat.mapper.PlaceBookingRecordMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/system/place")
public class PlaceBookingController {

    @Resource
    PlaceBookingRecordMapper placeBookingRecordMapper;

    @Resource
    UserInfoMapper userInfoMapper;

    @Resource
    NearbyPlacesMapper nearbyPlacesMapper;


    @GetMapping("/bookingRecords")
    public Result list(PlaceBookingRecord placeBookingRecord){
        Page<PlaceBookingRecord> placeBookingRecordPage = new Page<>(placeBookingRecord.getPageNum(),placeBookingRecord.getPageSize());
        QueryWrapper<PlaceBookingRecord> placeBookingRecordQueryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(placeBookingRecord.getBookingDate())){
            placeBookingRecordQueryWrapper.eq("booking_date",placeBookingRecord.getBookingDate());
        }
        if(StringUtils.isNotBlank(placeBookingRecord.getStatus())){
            placeBookingRecordQueryWrapper.eq("status",placeBookingRecord.getStatus());
        }
        if("wechat".equals(UserInfoContext.getUserInfo().getUserFrom())){
            placeBookingRecordQueryWrapper.eq("user_id",UserInfoContext.getUserInfo().getId());
        }
        Page<PlaceBookingRecord> result = placeBookingRecordMapper.selectPage(placeBookingRecordPage, placeBookingRecordQueryWrapper);
        for(int i=0;i<result.getRecords().size();i++){

        }
        return Result.success(result);
    }

    @GetMapping("/details/{id}")
    public Result getDetailsById(@PathVariable("id")Integer id){
        PlaceBookingRecord placeBookingRecord = placeBookingRecordMapper.selectById(id);
        UserInfo userInfo = userInfoMapper.selectById(placeBookingRecord.getUserId());
        placeBookingRecord.setUserName(userInfo.getUserName());
        return Result.success(placeBookingRecord);
    }

    @GetMapping("/courtDetails/{id}")
    public Result courtDetails(@PathVariable("id")Integer id){
        NearbyPlaces nearbyPlaces = nearbyPlacesMapper.selectById(id);
        return Result.success(nearbyPlaces);
    }

    /**
     * 小程序加载可预约信息
     * @param id
     * @return
     */
    @GetMapping("/bookedRecord")
    public Result detailsLoad(@RequestParam(required = true) Integer id,@RequestParam(required = false) String date){
        Map<String,Object> result = new HashMap<>();
        NearbyPlaces nearbyPlaces = nearbyPlacesMapper.selectById(id);
        if(nearbyPlaces==null){
            throw new BusinessException("当前数据不存在");
        }
        if(StringUtils.isBlank(date)){
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDate now = LocalDate.now();
            date = now.format(formatter);
        }
        QueryWrapper<PlaceBookingRecord> placeBookingRecordQueryWrapper = new QueryWrapper<>();
        placeBookingRecordQueryWrapper.eq("status","0");
        placeBookingRecordQueryWrapper.eq("booking_date",date);
        placeBookingRecordQueryWrapper.eq("place_id",id);
        List<PlaceBookingRecord> orderList = placeBookingRecordMapper.selectList(placeBookingRecordQueryWrapper);
        return Result.success(orderList);
    }

    /**
     * 同意或者拒绝
     * @param id
     * @param status
     * @return
     */
    @PostMapping("/approval/{id}/{status}")
    public Result approval(@PathVariable("id")Integer id,@PathVariable("status")String status){
        PlaceBookingRecord placeBookingRecord = placeBookingRecordMapper.selectById(id);
        if("0".equals(placeBookingRecord.getStatus())){
            placeBookingRecord.setStatus(status);
            placeBookingRecordMapper.updateById(placeBookingRecord);
            return Result.success();
        }
        return Result.fail("状态已变更，请刷新后查看");
    }

    @PostMapping("/cancelBooking/{id}")
    public Result cancelBooking(@PathVariable("id")Integer id){
        PlaceBookingRecord placeBookingRecord = placeBookingRecordMapper.selectById(id);
        if("0".equals(placeBookingRecord.getStatus())){
            placeBookingRecord.setStatus("1");
            placeBookingRecordMapper.updateById(placeBookingRecord);
            return Result.success();
        }
        return Result.fail("状态已变更，请刷新后查看");
    }
}
